package com.test.mytest.hessian;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.test.mytest.socket.User;


public class ServerServlet extends HttpServlet {

    private static final long serialVersionUID = 884523916637749569L;
    static Map<String,HttpServletResponse> map =new HashMap<String, HttpServletResponse>();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		try {
			String attribute = req.getParameter("type");
			System.out.println("attribute==========" + attribute);
			resp.setContentType("application/octet-stream");
			map.put(attribute, resp);
			int i=0;
			ServletOutputStream out = resp.getOutputStream();
			ObjectOutputStream o=new ObjectOutputStream(out);
			while (true) {
				ServletInputStream inputStream = req.getInputStream();
				if (inputStream.available()>0) {
					ObjectInputStream ois=new ObjectInputStream(inputStream);
					Object readObject = ois.readObject();
					System.out.println("servlet==="+readObject);
				}
				i++;
				User user = new User();
				user.setId(i*1l);
				user.setLoginName("aaa");
				user.setUserName("神秘人");
				user.setPassword("aaa");
				o.writeObject(user);
				out.flush();
				Thread.sleep(1000);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
    }

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}
}
